/**
* @Title: insuredBlacklist_ctrl.js
* @Description: TODO
* Copyright: Copyright (c) 2014 
 * Company:中国平安
*
* @Project HIMS-FWA
* @author EX-XIEYUWEN001
* @date 2014年10月29日 下午14:46:06
* @version V1.0
*/
;
(function(){
	'use strict';
	
	var app = angular.module('fwa.mainApp');
	app.controller('selectUserModalCtrl', ['$scope','CommunityService', '$http','BaseURL', 'Modal','$modalInstance','IntegrityMedicalService', function($scope,CommunityService, $http, baseUrl, Modal,$modalInstance,IntegrityMedicalService){
	
		
	    $scope.communityService = CommunityService;
        CommunityService.initCodeListDatas();
		
		$scope.query={};
		
		$scope.pageObj = {
                page:"1",
                pageSize:"20"
            };
		
	    $scope.checkRows = [];
		
		//表头
        $scope.modalColumns = [
							{label:'保险号',map:'insuranceNo',width:'15%'},
							{label:'姓名',map:'name',width:'10%'},
							{label:'性别',map:'gender',width:'7%'},
							{label:'年龄',map:'age',width:'7%'},
							{label:'出生日期',map:'birthdayDateStr',width:'15%'},
							{label:'医保卡号',map:'medicalCardNo',width:'15%'},
							{label:'身份证号',map:'idCard',width:'20%'},
							{label:'人员类别',map:'personType',width:'10%'}
							
						];
		
		
		var initDataUrl=baseUrl + 'integrityBlack/loadAllBlackPerson.do'; //加载所有的黑名单的信息URL
		var array=[];
	
	    /**
	     * 初始化modal的Table数据
	     */
	   function ininModalData(){
		   
		   
		   $http({
				method : 'post',
				cache : false,
				url : initDataUrl,
				params :getQueryParamter()
			}).success(function(response, status, headers, config) {
			         if(response.patientList){
			        	 $scope.modalTableData=response.patientList;
			        	 $scope.modalTotalCount=response.total;
			         }
			});
		   
	   }
	   
	   
	   //初始化数据
	   ininModalData();
	   
	   function getQueryParamter(){
		   var obj={
			    name:$scope.query.name,
			    personCard:$scope.query.personCard,
			    personType:$scope.query.personType,
			    cardNo:$scope.query.cardNo,
			    page:$scope.pageObj.page,
                pageSize:$scope.pageObj.pageSize
			    
		   };
		   
		   return obj;
	   }
	   
	   /**
	    * modal 的重置
	    */
	   $scope.onModalReset=function(){
		   $scope.query={};
	   }
	   
	   /**
	    * modal 的查询按钮
	    */
	   $scope.onModalSubmit=function(){
		   
		   ininModalData();
		   
	   }
	
		/**
		 * 关闭modal事件
		 */
		$scope.onClose = function() {
			$modalInstance.close();
		}
		
		/**
		 * 弹出框确定事件
		 */
		$scope.onModalConfirm=function(){
			
			var nameList=new Array();
			
			if($scope.checkRows.length>0){
				
				 for(var i=0;i< $scope.checkRows .length;i++) {
		                var checkedData =  $scope.checkRows [i];
		                
		                nameList.push(checkedData['name']);
		            }
				 
				 $scope.form.name=nameList.toString();
				 $scope.onClose();
				
			}else{
				 $scope.onClose();
			}
			
			
		}
		
		
		

	    function findCheck(d) {
	            for(var i=0;i< $scope.checkRows .length;i++) {
	                var checkedData =  $scope.checkRows [i];
	                if(checkedData.insuranceNo == d.insuranceNo) {
	                    return i;
	                }
	            }
	            return -1;
	        }

	        $scope.$on('fwatable.checked', function(e,d){
	            if(findCheck(d) == -1) {
	                $scope.checkRows.push(d);
	            }
	        });

	        $scope.$on('fwatable.unchecked', function(e,d){
	            var index = findCheck(d);
	            if(index != -1) {
	                $scope.checkRows.splice(index,1);
	            }
	        });
	        
	        
	    	//分页监听
			$scope.$on('pageTool.skipPage',function(e,v){
			    var id=e.targetScope.id;
			     if(id=='childPageTool'){
			    	   $scope.modalCurrentPage = v;
					    $scope.pageObj.page = v;
					    $scope.onModalSubmit();
			     }
				
			});
		

		
	}]);
})();